package cn.zmit.kuxi.activity;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import android.annotation.SuppressLint;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.fragment.GoodsFragmentCammer;
import cn.zmit.kuxi.fragment.GoodsFragmentComputer;
import cn.zmit.kuxi.fragment.GoodsFragmentOther;
import cn.zmit.kuxi.fragment.GoodsFragmentPhone;

/** * @author @parameter * @return */
@SuppressLint("ResourceAsColor")
public class GoodsActivity extends FragmentActivity {
	@ViewInject(R.id.ll)
	private LinearLayout mll;
	@ViewInject(R.id.goods_radio_button_group)
	private RadioGroup goods_radio_button_group;
	@ViewInject(R.id.bt_phone_o)
	private RadioButton bt_phone_o;
	@ViewInject(R.id.bt_computer_1)
	private RadioButton bt_computer_1;
	@ViewInject(R.id.bt_cammer_2)
	private RadioButton bt_cammer_2;
	@ViewInject(R.id.bt_orther_3)
	private RadioButton bt_orther_3;

	@ViewInject(R.id.view_indicator_phone)
	private View view_indicator_phone;
	@ViewInject(R.id.view_indicator_computer)
	private View view_indicator_computer;
	@ViewInject(R.id.view_indicator_cammer)
	private View view_indicator_cammer;
	@ViewInject(R.id.view_indicator_orther)
	private View view_indicator_orther;
	@ViewInject(R.id.ll_title)
	private LinearLayout ll_title;
	private FragmentManager fragmentManager;
	String data = null;
	// 显示商品的种类信息
	private GoodsFragmentPhone goodsFragmentPhone;
	private GoodsFragmentComputer goodsFragmentComputer;
	private GoodsFragmentCammer goodsFragmentCammer;
	private GoodsFragmentOther goodsFragmentOther;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_goods);
		ViewUtils.inject(this);
		Bundle bundle = getIntent().getExtras();
		if (bundle != null && bundle.size() > 0) {
			data = bundle.getString("flag");
			if (data != null && data.equals("首页")) {
				ll_title.setVisibility(View.VISIBLE);
			}
		} else {
			ll_title.setVisibility(View.GONE);
		}

		fragmentManager = getFragmentManager();
		setTabSelection(0);

	}

	// 顶部四个选项点击事件
	@OnClick({ R.id.bt_phone_o, R.id.bt_computer_1, R.id.bt_cammer_2,
			R.id.bt_orther_3, R.id.rl_back })
	public void OnClick(View v) {
		switch (v.getId()) {

		case R.id.bt_phone_o:
			toggleIndicator(R.id.bt_phone_o);
			setTabSelection(0);
			break;
		case R.id.bt_computer_1:
			toggleIndicator(R.id.bt_computer_1);
			setTabSelection(1);
			break;
		case R.id.bt_cammer_2:
			toggleIndicator(R.id.bt_cammer_2);
			setTabSelection(2);
			break;
		case R.id.bt_orther_3:
			toggleIndicator(R.id.bt_orther_3);
			setTabSelection(3);
			break;
		case R.id.rl_back:
			finish();
			break;
		default:
			break;
		}
	}

	private void toggleIndicator(int id) {
		switch (id) {
		case R.id.bt_phone_o:
			view_indicator_phone.setBackgroundColor(getResources().getColor(
					R.color.red2));
			view_indicator_computer.setBackgroundColor(getResources().getColor(
					R.color.gray));
			view_indicator_cammer.setBackgroundColor(getResources().getColor(
					R.color.gray));
			view_indicator_orther.setBackgroundColor(getResources().getColor(
					R.color.gray));

			break;
		case R.id.bt_computer_1:
			view_indicator_phone.setBackgroundColor(getResources().getColor(
					R.color.gray));
			view_indicator_computer.setBackgroundColor(getResources().getColor(
					R.color.red2));
			view_indicator_cammer.setBackgroundColor(getResources().getColor(
					R.color.gray));
			view_indicator_orther.setBackgroundColor(getResources().getColor(
					R.color.gray));
			break;
		case R.id.bt_cammer_2:
			view_indicator_phone.setBackgroundColor(getResources().getColor(
					R.color.gray));
			view_indicator_computer.setBackgroundColor(getResources().getColor(
					R.color.gray));
			view_indicator_cammer.setBackgroundColor(getResources().getColor(
					R.color.red2));
			view_indicator_orther.setBackgroundColor(getResources().getColor(
					R.color.gray));
			break;
		case R.id.bt_orther_3:
			view_indicator_phone.setBackgroundColor(getResources().getColor(
					R.color.gray));
			view_indicator_computer.setBackgroundColor(getResources().getColor(
					R.color.gray));
			view_indicator_cammer.setBackgroundColor(getResources().getColor(
					R.color.gray));
			view_indicator_orther.setBackgroundColor(getResources().getColor(
					R.color.red2));

			break;

		default:
			break;
		}
	}

	// 隐藏
	private void hideFragments(FragmentTransaction transaction) {
		if (goodsFragmentPhone != null) {
			transaction.hide(goodsFragmentPhone);
		}
		if (goodsFragmentComputer != null) {
			transaction.hide(goodsFragmentComputer);
		}
		if (goodsFragmentCammer != null) {
			transaction.hide(goodsFragmentCammer);
		}
		if (goodsFragmentOther != null) {
			transaction.hide(goodsFragmentOther);
		}
	}

	private void clear() {// 清除
		bt_phone_o.setTextColor(getResources().getColor(R.color.black));
		bt_computer_1.setTextColor(getResources().getColor(R.color.black));
		bt_cammer_2.setTextColor(getResources().getColor(R.color.black));
		bt_orther_3.setTextColor(getResources().getColor(R.color.black));
	}

	private void setTabSelection(int index) {
		clear();
		// 开启事务
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		// 隐藏所有fragment
		hideFragments(transaction);
		switch (index) {
		case 0:
			bt_phone_o.setTextColor(getResources().getColor(R.color.red2));
			if (goodsFragmentPhone == null) {
				goodsFragmentPhone = new GoodsFragmentPhone();
				transaction.add(R.id.content, goodsFragmentPhone);
			} else {
				transaction.show(goodsFragmentPhone);
			}
			break;
		case 1:
			bt_computer_1.setTextColor(getResources().getColor(R.color.red2));
			if (goodsFragmentComputer == null) {
				goodsFragmentComputer = new GoodsFragmentComputer();
				transaction.add(R.id.content, goodsFragmentComputer);
			} else {
				transaction.show(goodsFragmentComputer);
			}
			break;
		case 2:
			bt_cammer_2.setTextColor(getResources().getColor(R.color.red2));
			if (goodsFragmentCammer == null) {
				goodsFragmentCammer = new GoodsFragmentCammer();
				transaction.add(R.id.content, goodsFragmentCammer);
			} else {
				transaction.show(goodsFragmentCammer);
			}
			break;
		case 3:
			bt_orther_3.setTextColor(getResources().getColor(R.color.red2));
			if (goodsFragmentOther == null) {
				goodsFragmentOther = new GoodsFragmentOther();
				transaction.add(R.id.content, goodsFragmentOther);
			} else {
				transaction.show(goodsFragmentOther);
			}
			break;

		default:
			break;
		}
		// 提交
		transaction.commit();

	}

}
